# -*- coding: utf-8 -*-
"""
Created on Thu May 25 09:44:35 2017

@author: WANGWEIW23
"""
import numpy as np
import seaborn as sns
from PIL import Image
from PIL import ImageFile
from scipy import stats
import matplotlib as mpl  
import matplotlib.pyplot as plt
import os
from scipy.fftpack import*

def find_threshould(fft_image_1,interval):
    pdf=[]
    num=len(fft_image_1)
    maxid=max(fft_image_1)
    for i in range(0,int(maxid),int(maxid/interval)):
        cond=(fft_image_1>i)&(fft_image_1<i+int(maxid/interval))
        arr_2=len(list(np.extract(cond,fft_image_1)))
        pdf.append(arr_2/num)    
    pdf=[(pdf.index(max(pdf)))*int(maxid/interval)+int(maxid/interval)/2]
    return pdf

class fft(object):   
    def __init__(self):
        self.filelist=[]
        self.graylist=[]
        self.image_1_list=[]
        self.fft_image_1_list=[]
        self.curve=[]
    
    def eachFile(self,filepath):
        filelist=[]
        pathDir = os.listdir(filepath)
        for allDir in pathDir:
            child = os.path.join('%s%s%s' % (filepath,'/', allDir))
            filelist.append(child)
           # .decode('gbk')是解决中文显示乱码问题
        self.filelist=filelist

###二维度的离散傅里叶变换，以及对应做出对应的频率分布图形        
    def color_to_gray(self,filelist):
         graylist=[]
         try:
             for i in range(len(filelist)):             
                 temp=plt.imread(filelist[i])
                 gray=temp[:,:,0]*0.299+temp[:,:,1]*0.587+temp[:,:,2]*0.114
                 graylist.append(gray)
         except IndexError:
             pass           
         self.graylist=graylist
    
    def index_plot(self,graylist):
        image_1_list=[]
        fft_image_1_list=[]
        for k in range(len(graylist)):            
            fft_image=fft2(graylist[k])
            [h,w]=graylist[k].shape
            image_1_temp=[]
            fft_image_1_temp=[]
            image_1=np.zeros(h*w) 
            fft_image_1=np.zeros(h*w)
        
            for i in range(h):
                for j in range(w):
                    image_1_temp.append(graylist[k][i,j])
                    fft_image_1_temp.append(fft_image[i,j])        
            for s in range(h*w):
                image_1[k]=image_1_temp[s]
                fft_image_1[s]=abs(fft_image_1_temp[s])
            image_1_list.append(image_1)
            fft_image_1_list.append(fft_image_1)
            self.image_1_list=image_1_list
            self.fft_image_1_list=fft_image_1_list
        
    def curve_draw(self,fft_image_1_list):
        pdf=[]
        for mx in fft_image_1_list:
            pdf.append(find_threshould(mx,1000))
        self.curve=pdf
####通过寻找傅里叶变换之后幅值的集中程度比较高的区间来区分

    
if __name__ == '__main__':
    path="D:\\output\\grid"
    model=fft()
    model.eachFile(path)
    model.color_to_gray(model.filelist)
    model.index_plot(model.graylist)
    model.curve_draw(model.fft_image_1_list)
    a=[1,9,18]
    for i in a:
        sns.kdeplot(model.fft_image_1_list[i], shade=True, clip=(0,50000))
#    plt.hist(model.curve, 30, normed=1, facecolor='blue', alpha=0.5)
#    sns.kdeplot(model.fft_image_1, shade=True) 
##    plt.savefig('test.png',dpi=100)
#    plt.show()
#    model.find_threshould(model.fft_image_1)
#    print(model.pdf)
    ####通过统计特性来区分傅里叶变换之后幅值
#    np.mean(fft_image_1)
#    np.median(fft_image_1)
#    plt.subplot(211)
#    sns.kdeplot(model.image_1, shade=True)
#    plt.subplot(212)
#    sns.kdeplot(model.fft_image_1, shade=True) 
     

    
    